package com.example.myretrofit2.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.example.myretrofit2.GoodsInfo;
import com.example.myretrofit2.common.MyApplication;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

public class ViewpagerAdapter extends PagerAdapter {
List<GoodsInfo> list;
Context context;

    public ViewpagerAdapter(List<GoodsInfo> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view==object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        ImageView imageView = new ImageView(context);
        ImageLoader.getInstance().displayImage(MyApplication.imageUrl+list.get(position).getSrc(),imageView);
        container.addView(imageView);
        return imageView;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }
}
